package logic

import (
	"context"

	"gb28181/gb28181"
	"gb28181/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterDeviceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterDeviceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterDeviceLogic {
	return &RegisterDeviceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterDeviceLogic) RegisterDevice(in *gb28181.DeviceRegisterRequest) (*gb28181.DeviceRegisterResponse, error) {
	// 使用客户端管理器注册设备
	_, err := l.svcCtx.ClientMgr.Register(l.ctx, in.DeviceId, in.SipServerId, in.SipUri)
	if err != nil {
		l.Errorf("设备注册失败: %v", err)
		return &gb28181.DeviceRegisterResponse{
			Success:  false,
			Message:  "设备注册失败: " + err.Error(),
			DeviceId: in.DeviceId,
		}, nil
	}

	// 记录设备信息
	l.Infof("设备注册成功: %s, SIP URI: %s", in.DeviceId, in.SipUri)

	return &gb28181.DeviceRegisterResponse{
		Success:  true,
		Message:  "设备注册成功",
		DeviceId: in.DeviceId,
	}, nil
}